﻿from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from views import router as auth_router
from views.recommendation_views import app_router as recommendation_router
from views.content_views import router as content_router
from models import create_tables, init_tags
import uvicorn

# 创建FastAPI应用
app = FastAPI(
    title="趣享生活 API",
    description="小红书原型项目 - 用户登录模块 + 内容推荐系统",
    version="2.0.0"
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000", 
        "http://localhost:5173", 
        "http://localhost:5174",
        "http://localhost:5175"  # 前端可能运行的端口
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(auth_router, prefix="/api/auth", tags=["认证"])
app.include_router(auth_router, prefix="/api", tags=["标签"])
app.include_router(recommendation_router, prefix="/api", tags=["推荐"])
app.include_router(content_router, prefix="/api", tags=["内容"])

# 创建数据库表
create_tables()

# 初始化标签数据
init_tags()

@app.get("/")
async def root():
    return {
        "message": "趣享生活 API 服务运行中",
        "version": "2.0.0",
        "docs": "/docs",
        "features": ["用户认证", "兴趣标签", "内容推荐", "Elasticsearch集成"]
    }

@app.get("/health")
async def health_check():
    return {"status": "healthy"}

if __name__ == "__main__":
    print("=== 趣享生活后端服务启动 ===")
    print("服务地址: http://localhost:8000")
    print("API文档: http://localhost:8000/docs")
    print("健康检查: http://localhost:8000/health")
    print("推荐功能: http://localhost:8000/api/recommendations")
    print("按 Ctrl+C 停止服务")
    print("=" * 40)
    
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )
